package com.Policing.Polling;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.Policing.Adapter.Public_list_item_Adapter;
import com.Policing.BaseActivity;
import com.Policing.Ip_set.Ip_Set;
import com.Policing.Okhttps.HttpOkHttp;
import com.Policing.R;
import com.Policing.Since_case.Since_case;
import com.hb.dialog.myDialog.MyAlertInputDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sakura.bottommenulibrary.bottompopfragmentmenu.BottomMenuFragment;
import sakura.bottommenulibrary.bottompopfragmentmenu.MenuItem;

public class Polling_path extends BaseActivity {
    private ListView checklist;
    private TextView title_text;
    private LinearLayout iput;
    private ImageView fh;
    private List<String> routeIds  = new ArrayList<>();
    private List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
    private String id="",roleId="",version="",routeId="";
    private String url = "http://" + Ip_Set.IP + "/api/routeActivate";
    private String routeTimeOut = "http://" + Ip_Set.IP + "/api/routeTimeOut";
    @Override
    public int intiLayout() {
        return R.layout.polling_path;
    }

    @Override
    public void initView() {
        iput = (LinearLayout) findViewById(R.id.iput);
        iput.setVisibility(View.GONE);
        checklist = (ListView) findViewById(R.id.checklist);
        title_text = (TextView) findViewById(R.id.title_text);
        fh = (ImageView) findViewById(R.id.fh);
        title_text.setText("路线");
        fh.setOnClickListener(v -> finish());
        checklist.setOnItemClickListener((parent, view, i, id) -> {
            if (version.equals("1")){
                if (data.get(i).get("scope").equals("超时")){
                    final MyAlertInputDialog myAlertInputDialog = new MyAlertInputDialog(Polling_path.this).builder()
                            .setTitle("提示！！！")
                            .setEditText("请输入超时原因！");
                    myAlertInputDialog.setPositiveButton("确认", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TimeOut(routeIds.get(i),myAlertInputDialog.getResult(),i);
                            myAlertInputDialog.dismiss();
                        }
                    }).setNegativeButton("取消", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            myAlertInputDialog.dismiss();
                        }
                    });
                    myAlertInputDialog.show();

                }
                if (data.get(i).get("scope").equals("未激活")){
                    new BottomMenuFragment(Polling_path.this)
                            .addMenuItems(new MenuItem("激活"))
                            .setOnItemClickListener((menu_item, position) -> {
                                if (position==0){
                                    routeId = routeIds.get(i);
                                    activate(i);

                                }
                                if (position==1){

                                }
                            })
                            .show();
                }else if (data.get(i).get("scope").equals("激活")){
                    new BottomMenuFragment(Polling_path.this)
                            .addMenuItems(new MenuItem("发布任务"))
                            .setOnItemClickListener((menu_item, position) -> {
                                if (position==0){
                                   /* routeId = routeIds.get(i);
                                    activate(i);*/
                                    Intent intent = new Intent(Polling_path.this,taskRelease.class);
                                    intent.putExtra("items",String.valueOf(i));
                                    intent.putExtra("routeId",routeIds.get(i));
                                    startActivityForResult(intent,2);

                                }
                                if (position==1){

                                }
                            })
                            .show();
                }
            }else if (version.equals("2")){
                if (data.get(i).get("scope").equals("超时")){
                    final MyAlertInputDialog myAlertInputDialog = new MyAlertInputDialog(Polling_path.this).builder()
                            .setTitle("提示！！！")
                            .setEditText("请输入超时原因！");
                    myAlertInputDialog.setPositiveButton("确认", v -> {
                        TimeOut(routeIds.get(i),myAlertInputDialog.getResult(),i);
                        myAlertInputDialog.dismiss();
                    }).setNegativeButton("取消", v -> myAlertInputDialog.dismiss());
                    myAlertInputDialog.show();

                }
                if (data.get(i).get("scope").equals("激活")){
                    new BottomMenuFragment(Polling_path.this)
                            .addMenuItems(new MenuItem("发布任务"))
                            .setOnItemClickListener((menu_item, position) -> {
                                if (position==0){
                                    Intent intent = new Intent(Polling_path.this,taskRelease.class);
                                    intent.putExtra("items",String.valueOf(i));
                                    intent.putExtra("routeId",routeIds.get(i));
                                    startActivityForResult(intent,2);

                                }
                                if (position==1){

                                }
                            })
                            .show();
                }else if (data.get(i).get("scope").equals("未激活")){

                }

            }



        });
    }

    @Override
    public void initData() {
        SharedPreferences mm = getSharedPreferences("mm", Context.MODE_PRIVATE);//1.通过上下文得到
        id = mm.getString("id", "");
        roleId = mm.getString("roleid","");
        version = mm.getString("version","");
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        data = (List<Map<String, Object>>) bundle.getSerializable("routes");
        routeIds = (List<String>) bundle.getSerializable("routeId");
        Public_list_item_Adapter re_ad = new Public_list_item_Adapter(Polling_path.this,data);
        checklist.setAdapter(re_ad);

    }
    private void activate(final int i){
        Map<String,String> map = new HashMap<>();
        map.put("routeId",routeId);
        map.put("source",Since_case.set().getSource());
        HttpOkHttp.getInstance().requestPost(url, map, new HttpOkHttp.OkHttpCallBack<String>() {
            @Override
            public void requestSuccess(final String s) {
                runOnUiThread(() -> {
                    try {

                        JSONObject jsonObject = new JSONObject(s);
                        if (jsonObject.getString("state").equals("1")){
                            toastShort("激活成功");
                            data.get(i).put("scope","激活");
                            Public_list_item_Adapter re_ad = new Public_list_item_Adapter(Polling_path.this,data);
                            checklist.setAdapter(re_ad);
                        }else if (jsonObject.getString("state").equals("0")){

                            toastShort("激活失败");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                });

            }

            @Override
            public void requestFailure(String message) {

            }
        },String.class);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent sd) {
        super.onActivityResult(requestCode, resultCode, sd);
        if (requestCode==2&&resultCode==2){
            String s= sd.getExtras().getString("items");
            if (version.equals("1")){
                data.get(Integer.parseInt(s)).put("scope","未激活");
                Public_list_item_Adapter re_ad = new Public_list_item_Adapter(Polling_path.this,data);
                checklist.setAdapter(re_ad);
            }else if (version.equals("2")){
              data.remove(Integer.parseInt(s));
                Public_list_item_Adapter re_ad = new Public_list_item_Adapter(Polling_path.this,data);
                checklist.setAdapter(re_ad);
            }

        }
    }
    private  void TimeOut(String routeId, String remark, final int i){
        Map<String,String> map = new HashMap<>();
        map.put("remark",remark);
        map.put("routeId",routeId);
        //加了source
        map.put("source", Since_case.set().getSource());
        HttpOkHttp.getInstance().requestPost(routeTimeOut, map, new HttpOkHttp.OkHttpCallBack<String>() {
            @Override
            public void requestSuccess(final String s) {
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject.getString("state").equals("1")){
                       runOnUiThread(() -> {
                           toastShort("提交成功");
                           data.get(i).put("scope","激活");
                           Public_list_item_Adapter re_ad = new Public_list_item_Adapter(Polling_path.this,data);
                           checklist.setAdapter(re_ad);
                       });
                    }
                    if (jsonObject.getString("state").equals("2")){
                      runOnUiThread(() -> toastShort("提交失败"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(String message) {

            }
        },String.class);
    }
}
